package offer;

import java.util.LinkedList;

/**
 * 关键在于压入和弹出时的判断. 两个栈min 和data
 * @author fance
 * @date 18-4-25 上午9:06
 */

public class MinStack {
    private LinkedList<Integer> data;
    private LinkedList<Integer> min;

    public MinStack(LinkedList<Integer> data, LinkedList<Integer> min) {
        this.data = data;
        this.min = min;
    }
    public void push(int item) {
        if (min.isEmpty()) {
            min.push(item);
        } else if (item <= getMin()) {
            min.push(item);
        }
        data.push(item);
    }

    public int pop() {
        if (data.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        int val = data.pop();
        if (val == getMin()) {
            min.pop();
        }
        return val;
    }
    public int getMin() {
        if (min.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        return min.peek();
    }
}
